<div class="content">
    <div class="form-row row-fluid" id="div_form_facture">
        {% include "GdsVenteBundle::Facture/form_facture.html.twig" %}

        </div>

    </div>
    <script type="text/javascript">
        Array.prototype.remove = function(v) {
            this.splice(this.indexOf(v) == -1 ? this.length : this.indexOf(v), 1);
        }
        $(document).ready(function() {
            $('#departement').val({{user.departement.id}})
            $('#departement').attr('disabled', true);
            articles = new Array();
            mdp = ""
            $('#start').show()
            $('#now').val('{{time}}')
            $('#heure').val('{{heure}}')
            $('#start').click(function() {
                $(this).hide()
                $('#prev').show()
                timerId = setInterval(time, 1000);
            })
            $('#prev').click(function() {
                $(this).hide()
                $('#start').show()
                clearInterval(timerId);
                $('#now').val('{{time}}')
                $('#heure').val('{{heure}}')
            })
            $('#retour2').click(function() {
                clearInterval(timerId);
            })
            $('#submit_form_facture').click(function() {
                if ($('#submit_form_facture').val() != 'Duppliquer') {
                    if ($('#form_facture').valid()) {
                        $.ajax({
                            url: '{{path("gds_vente_facture_edit")}}',
                            type: "POST",
                            datatype: 'json',
                            data: $('#form_facture').serialize() + '&articles={"articles":' + JSON.stringify(articles) + '}&heure=' + $('#heure').val() + '&id=' + '{{id}}' + '&mdp=' + mdp,
                            success: function(data) {
                                if (data.success) {
                                    $.msgBox({
                                        title: "Facture",
                                        content: "Enregistrement Effectué avec succés",
                                        type: "info",
                                        buttons: [{value: "OK"}],
                                        success: function(result) {
                                            location.reload();
                                        }
                                    });
                                } else if (data.limit_depass) {
                                    $.msgBox({
                                        title: "Dépassement du Solde Client !!",
                                        content: "mdp: <input id='mdp' type='password' />",
                                        type: "confirm",
                                        buttons: [
                                            {type: "submit", value: "Valider"},
                                            {type: "cancel", value: "Annuler"}
                                        ],
                                        afterShow: function() {
                                            $('#mdp').focus()
                                        },
                                        success: function(result) {
                                            if (result == "Valider") {
                                                if ($('#mdp').val() != '') {
                                                    mdp = $('#mdp').val()
                                                    $('#submit_form_facture').click()
                                                }
                                            }
                                        }
                                    });
                                } else {
                                    $.msgBox({
                                        title: "Facture",
                                        content: "Erreur D'enregistrement",
                                        type: "error",
                                        buttons: [{value: "OK"}]
                                    });
                                }
                            },
                            error: function(e) {
                                $.msgBox({
                                    title: "Facture",
                                    content: "Erreur D'enregistrement",
                                    type: "error",
                                    buttons: [{value: "OK"}],
                                    success: function(result) {

                                    }
                                });
                            }
                        })
                    }
                }
            })
            $('#gds_ventebundle_bonlivraisontype_date').datepicker();
            $('#form_facture').validate();
            $container = $('#gds_ventebundle_lignebonlivraisonclienttype');
            index = 0;
            nb_article = 1;
            {% for l,ligne in lignes %}
            add_ligne()
            $('#td_reference_' +{{l}}).html('{{ligne.article.reference}}')
            $('#td_designation_' +{{l}}).html('{{ligne.article.designation}}')
            $('#gds_ventebundle_' +{{l}} + '_quantite').val('{{ligne.quantite}}')
            setQuantite({{l}})
            $('#td_prix_unitaire_ht_' +{{l}}).html('{{ligne.prixUnitaireHt}}')
            tva_twig = {{ligne.tauxTva}}
            tva_twig = tva_twig.toFixed(2)
            $('#td_tva_' +{{l}}).html(tva_twig + '%')
            $('#gds_ventebundle_' +{{l}} + '_taux_remise').val('{{ligne.tauxRemise}}')
            $('#gds_ventebundle_' +{{l}} + '_article').val('{{ligne.article.id}}')
            var x = new Object();
            x.reference = $('#td_reference_' + {{l}}).html()
            x.designation = $('#td_designation_' + {{l}}).html()
            x.taux_tva = '{{ligne.tauxTva}}'
            x.remise = '{{ligne.tauxRemise}}'
            x.remise_max = '{{ligne.article.remiseMax}}'
            x.prix_vente = '{{ligne.prixUnitaireHt}}'
            x.quantite = '{{ligne.quantite}}'
            x.prix_vente_min_ttc = x.prix_vente * (1 + (x.taux_tva / 100)) * (1 - (x.remise_max / 100))
            x.name = x.reference + ' : ' + x.designation
            x.id = '{{ligne.article.id}}'
            articles[{{l}}] = x
            completerChamp('{{l}}')
            {% endfor %}
            RefraichirTaxe()
        });
        function completerChamp(i) {
            var article = getArticle($('#gds_ventebundle_' + i + '_article').val())
            quantite = article.quantite;
            prix_vente = article.prix_vente;
            tva = article.taux_tva;
            remise = article.remise;
            puttc = prix_vente
            puttc *= (1 + tva / 100)
            puttc *= (1 - remise / 100)
            $('#td_prix_u_ttc_' + i).html(puttc.toFixed(3));
            total = puttc * quantite
            $('#td_total_' + i).html(total.toFixed(3));
            $('#gds_ventebundle_' + i + '_taux_remise').spinner({
                min: 0,
                max: article.remise_max,
                step: 1,
                places: 2,
                showOn: 'both'
            });
        }

        function retourListeDevis2() {
            $.ajax({
                url: "{{ path('gds_vente_liste_facture') }}",
                data: 'retour=true' + '&numero=' + lf[0] + '&raison_social=' + lf[1] + '&date1=' + lf[2] + '&date2=' + lf[3] + '&heure1=' + lf[4] + '&heure2=' + lf[5]
                        + '&reference=' + lf[6] + '&designation=' + lf[7] + '&ttc1=' + lf[8] + '&ttc2=' + lf[9] + '&is_cloture=' + lf[10],
                success: function(data) {
                    $('#div_liste_facture').replaceWith(data)
                    $('#filtre_numero').val(lf[0])
                    $('#filtre_raison_social').val(lf[1])
                    $('#filtre_date1').val(lf[2])
                    $('#filtre_date2').val(lf[3])
                    $('#filtre_reference').val(lf[6])
                    $('#filtre_designation').val(lf[7])
                    $('#filtre_ttc1').val(lf[8])
                    $('#filtre_ttc2').val(lf[9])
                }
            })
        }
        </script>

        <input type="hidden" id="heure"/>
        <input type="button" value="article" id="show_article">